package task

import (
	"GoPro/Server/msgProto"
	"GoPro/Server/mynet"
	"GoPro/Server/protoMsg"
	"encoding/binary"
	"fmt"
	"google.golang.org/protobuf/proto"
)

type LoginRouter struct {
}

func (msg *LoginRouter) PreHandler(req mynet.IRequest) bool {
	//fmt.Println(string(req.GetData()))
	return true
}

func (msg *LoginRouter) Handler(req mynet.IRequest) bool {
	result := msgProto.MsgHeader{
		MsgType: -1,
		MsgSize: 0,
	}

	loginMsg := protoMsg.Login{}
	proto.Unmarshal(req.GetData(), &loginMsg)
	fmt.Println("username:", loginMsg.Username, " password", loginMsg.Password, " login success")
	server := req.GetServer()
	server.OnlineUserLock.Lock()
	server.OnlineUser[req.GetIConnection()] = string(loginMsg.Username)
	binary.Write(req.GetIConnection().GetWriter(), binary.BigEndian, result)
	defer server.OnlineUserLock.Unlock()
	return true
}

func (msg *LoginRouter) PostHandler(req mynet.IRequest) bool {
	fmt.Println(fmt.Sprintf("post ok msgType:%d", req.GetType()))
	return true
}
